package com.cuit.bysj.InterfaceAction;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import com.cuit.bysj.entities.User;
import com.cuit.bysj.service.UserService;
import com.opensymphony.xwork2.ActionSupport;

public class androidLogin extends ActionSupport {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private Map<String, Object> dataMap;
	private UserService userService;
	private String username;
	private String password;
	private String id;

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public String islogin() throws IOException {
		User user = userService.isLogin(username, password);

		// dataMap中的数据将会被Struts2转换成JSON字符串，所以这里要先清空其中的数据
		dataMap = new HashMap<String, Object>();
		if (user != null) {
			dataMap.put("user", user);
			// 如果是学生用户，生成评价表
			if (user.getRole().getRoleName().equals("学生")) {
				userService.initform(user.getStu());
			}
			userService.initState();

		} else {
			dataMap.put("user", null);
		}
		// 返回结果

		return "json";
	}

	public String updateUser() {
		User user = userService.getUserbyid(Integer.parseInt(id));
		user.setPassword(password);

		userService.updateUser(user);

		dataMap = new HashMap<String, Object>();
		dataMap.put("user", user);
		
		return "json";
	}

	public Map<String, Object> getDataMap() {
		return dataMap;
	}

}
